'use strict';
const db = uniCloud.database()
const dbCmd = db.command
exports.main = async (event, context) => {
	//event为客户端上传的参数
	console.log('event : ', event)
	// let memberId = 1018
	// let memberRes = await db.collection("uni-id-users").where({
	// 	id: memberId
	// }).get()
	// if (!memberRes.data.length) {
	// 	console.error("用户不存在")
	// 	return
	// }
	// let member = memberRes.data[0]
	let res = await db.collection('file-cache')
		.where({
			srcUrl: new RegExp(`goods-poster_`)
		})
		.limit(50)
		.get()
	var fileIDs = res.data.map(item => {
		return item.fileID
	})
	// let posterRes = await db.collection("file-cache")
	// 	.where({
	// 		srcUrl: new RegExp(`^poster_${member._id}`)
	// 	})
	// 	.get()
	// fileIDs = fileIDs.concat(posterRes.data.map(item => {
	// 	return item.fileID
	// }))
	console.log('fileIDs', fileIDs)
	if (!fileIDs.length) {
		return
	}
	let delRes = await uniCloud.deleteFile({
		fileList: fileIDs
	})
	console.log('delRes', delRes)
	let delGoodsPosterCacheRes = await db.collection('file-cache')
		.where({
			fileID: dbCmd.in(fileIDs)
		}).remove()
	//返回数据给客户端
	return event
};
